package com.igeek.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.entity.Incomingstock;
import com.igeek.boot.entity.Shelves;
import com.igeek.boot.mapper.IncomingstockMapper;
import com.igeek.boot.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.boot.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@Service
public class IncomingstockServiceImpl extends ServiceImpl<IncomingstockMapper, Incomingstock> implements IIncomingstockService {


    @Autowired
    private IAssetsService assetsService;

    @Autowired
    private IShelvesService shelvesService;

    @Autowired
    private IWarehousesService warehousesService;

    @Autowired
    private ISuppliersService suppliersService;

    //添加入库订单信息
    @Transactional
    @Override
    public Boolean addIncomingStockInfo(Incomingstock incomingstock) {
        //1.随机生成订单号
        String orderNumber = "I"+ UUID.randomUUID();
        incomingstock.setIncomingNumber(orderNumber);
        //2.进行入库表的添加操作
        int row = baseMapper.insert(incomingstock);//3.进行物资表的添加操作  -->
        return row>0;
    }

    //确认入库订单操作
    @Transactional
    @Override
    public Boolean confirmIncomingStock(Incomingstock incomingstock) {
        //1.根据货架id查询物资信息并在物资信息表中寻找和当前入库的物资名和类型一致的物资
        Assets asset = assetsService.selectByShelfIdAndNameType(incomingstock.getShelfID(),incomingstock.getIncomingName(),incomingstock.getIncomingType());
        //2.存在当前物资，就进行数量的累加

        if(ObjectUtils.isNotEmpty(asset)){
            //3.如果存在该物资，就进行数量的累加
            asset.setQuantity(asset.getQuantity() + incomingstock.getIncomingQuantity());
            if(!assetsService.updateById(asset)){
                return false;
            }
        }else{
            //3.不存在该订单的物资，进行添加物资信息
            Assets newAsset = new Assets();
            newAsset.setAssetName(incomingstock.getIncomingName());
            newAsset.setAssetType(incomingstock.getIncomingType());
            newAsset.setQuantity(incomingstock.getIncomingQuantity());
            newAsset.setShelfId(incomingstock.getShelfID());
            //添加失败就返回false
            if(!assetsService.add(newAsset)){
                return false;
            }
        }
        //4.更新入库的订单状态
        int row = baseMapper.updateById(incomingstock);
        return  row>0;
    }

    //入库订单分页查询
    @CachePut(cacheNames = "incoming",keyGenerator = "keyGenerator")
    @Transactional(readOnly = true)
    @Override
    public Page<Incomingstock> findPage(PageQueryVO pageQueryVO) {
        //封装分页条件
        Page<Incomingstock> page = new Page<>(pageQueryVO.getCurrentPage(), pageQueryVO.getPageSize());

        //封装查询条件  根据物资名称和类型进行查询
        LambdaQueryWrapper<Incomingstock> lqw = new LambdaQueryWrapper<>();
        lqw.or().like(StringUtils.hasLength(pageQueryVO.getQueryString()),Incomingstock::getIncomingName,pageQueryVO.getQueryString())
                .or()
                .like(StringUtils.hasLength(pageQueryVO.getQueryString()),Incomingstock::getIncomingType,pageQueryVO.getQueryString());
        //执行查询
        Page<Incomingstock> pageInfo = baseMapper.selectPage(page,lqw);
        //针对查询的records进行手动添加货架名称和仓库名称
        List<Incomingstock> collect = pageInfo.getRecords().stream().map(item -> {
            if (ObjectUtils.isNotEmpty(item.getShelfID())) {
                //获取货架信息 -->  填写货架名  --> 设置货架的仓库名
                Shelves shelves = shelvesService.getById(item.getShelfID());
                item.setShelfName(shelves.getShelfName());
                item.setWarehouseName(warehousesService.getById(shelves.getWarehouseId()).getWarehouseName());
                //获取入库的供应商信息 --> 设置供应商名称
                String supplierName = suppliersService.getById(item.getIncomingSupplierID()).getSupplierName();
                item.setSupplierName(supplierName);
            }
            return item;
        }).collect(Collectors.toList());
        pageInfo.setRecords(collect);
        return pageInfo;
    }
}
